package com.av.record

import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.av.recorder.activity.RecordedActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btn_video.setOnClickListener {
            getCameraPermission()
        }

        input_btn.setRecordView(input_audio)

        getMasterPermission()
    }

    fun getMasterPermission(){
        PermissionUtil.getPermission(arrayOf(
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        ),this,true,object :PermissionUtil.VerifyPermissionsCallback{
            override fun onPermissionAllGranted() {

            }

            override fun onPermissionDeny(permissions: Array<out String>?) {
                getMasterPermission()
            }

        })
    }

    @SuppressLint("SetTextI18n")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            if (requestCode == 0x211) {
                val dataType = data!!.getIntExtra(
                    RecordedActivity.INTENT_DATA_TYPE,
                    RecordedActivity.RESULT_TYPE_VIDEO
                )
                if (dataType == RecordedActivity.RESULT_TYPE_VIDEO) {
                    val mFilePath = data.getStringExtra("intent_path")
                    sendBroadcast(
                        Intent(
                            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                            Uri.parse("file://$mFilePath")
                        )
                    )
                    tv_video_path.text = "视频: $mFilePath";
                } else if (dataType == RecordedActivity.RESULT_TYPE_PHOTO) {
                    val photoPath =
                        data.getStringExtra(RecordedActivity.INTENT_PATH)
                    tv_video_path.text="图片地址: $photoPath";
                }
            }
        }
    }

    private fun getCameraPermission() {
        if (!PermissionUtil.getPermission(arrayOf(
                Manifest.permission.CAMERA,
                Manifest.permission.RECORD_AUDIO
            ),
                this, true,
                object : PermissionUtil.VerifyPermissionsCallback {
                    override fun onPermissionAllGranted() {
                        showTakePhotoDialog()
                    }

                    override fun onPermissionDeny(permissions: Array<out String>?) {
                    }

                })
        ) {
            showTakePhotoDialog()
        }
    }

    fun showTakePhotoDialog(){
        val intent = Intent()
        try {
            val c = Class.forName("com.av.recorder.activity.RecordedActivity")
            intent.setClass(this, c)
            intent.putExtra("type", -1)//-1，视频；0，视频与拍照；1，拍照
            intent.putExtra("maxTime", 60)
            startActivityForResult(intent, 0x211)
        } catch (e: Exception) {
            Log.d("------", e.message!!)
        }
    }
}